package tgipl3.medium.buildingBlocks.minified;
import java.io.IOException;
import java.util.*;
public class UserMainCodeV1
{
	public static int output1[], o = 0;
	private static C[][] q;
	
	private static class C extends ArrayList<N> implements Comparable<C> {
		private static final long serialVersionUID = 1L;
		int c = 0, h = 0;
		static final int F = -1;
		public C(N n, int h) {
			add(n);
			this.h = h;
		}
		public int compareTo(C t) {
			return h - t.h;
		}
	}
	
	private static class N {
		int r, c;
		public N(int r, int c) {
			this.r = r;
			this.c = c;
		}
		public boolean equals(Object o) {
			if(o instanceof N)
				return r == ((N)o).r && c == ((N)o).c;
			return false;
		}
	}

	private static int[][] p(String t) {
		int c = t.indexOf(',');
		int l = Integer.parseInt(t.substring(0, c));
		t = t.substring(c + 1);
		c = t.indexOf(',');
		int b = Integer.parseInt(t.substring(0, c));
		t = t.substring(c + 3, t.length() - 2);
		int p[][] = new int[l][b];
		String r[] = t.split("\\},\\{");
		for(int i = 0; i < r.length; i++) {
			String e[] = r[i].split(",");
			for(int j = 0; j < e.length; j++)
				p[i][j] = Integer.parseInt(e[j]);
		}
		return p;
	}
	
	private static List<C> g(int[][] p) {
		ArrayList<C> c = new ArrayList<C>();
		for(int i = 1; i < p.length - 1; i++) {
			for(int j = 1; j < p[i].length - 1; j++) {
				if(p[i - 1][j] >= p[i][j] && p[i][j - 1] >= p[i][j]
						&& p[i][j + 1] >= p[i][j] && p[i + 1][j] >= p[i][j]) {
					C d = new C(new N(i, j), p[i][j]);
					c.add(d);
					q[i][j] = d;
				}
			}
		}
		return c;
	}
	
	private static void m(C c, C d) {
		c.addAll(d);
		c.c = Math.min(c.c, d.c);
	}
	
	private static void e(List<C> t, C v, int[][] p, N n, int r, int c) {
		N w = new N(r, c);
		if(p[n.r][n.c] >= p[r][c]) {
			if(!v.contains(w)) {
				if(q[r][c] != null && q[r][c] != v) {
					m(v, q[r][c]);
					t.remove(q[r][c]);
				} else
					v.add(w);
				q[r][c] = v;
			}
		} else {
			int f = p[r][c] - p[n.r][n.c];
			if(v.c == C.F || f < v.c)
				v.c = f;
		}
	}
	
	private static boolean e(List<C> t, C c, int[][] p) {
		c.c = C.F;
		for(int i = 0; i < c.size(); i++) {
			N n = c.get(i);
			if(n.r == 0 || n.r == p.length - 1
					|| n.c == 0 || n.c == p[0].length - 1) {
				t.remove(c);
				return false;
			}
			e(t, c, p, n, n.r - 1, n.c);
			e(t, c, p, n, n.r, n.c - 1);
			e(t, c, p, n, n.r, n.c + 1);
			e(t, c, p, n, n.r + 1, n.c);
		}
		return true;
	}
	
	private static void f(C c, int[][] p) {
		int m = c.h + c.c;
		for(N n : c)
			p[n.r][n.c] = m;
		c.h = m;
		output1[o] += c.c * c.size();
	}
	
	public static void GetWaterLevel(int i, String j)
	{
		String t[] = j.substring(1, j.length() - 1).split("\\),\\(");
		output1 = new int[t.length];
		for(String v : t) {
			int[][] p = p(v);
			q = new C[p.length][p[0].length];
			List<C> c = g(p);
			while(c.size() > 0) {
				Collections.sort(c);
				C m = c.get(0);
				if(e(c, m, p))
					f(m, p);
			}
			o++;
		}
	}
	
	public static void main(String[] args) throws IOException
	{
		String[] inputs = "1:(3,6,{{3,3,4,4,4,2},{3,1,3,2,1,4},{7,3,1,6,4,1}})".split(":");
		GetWaterLevel(Integer.parseInt(inputs[0]), inputs[1]);
		System.out.println(Arrays.toString(output1));
	}
}